<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加数据</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/x_mgr/start/layui/css/layui.css" media="all">
    <style>
        body {
            padding: 10px;
            font-size: 14px;
            background: #fff;
            width: 95%;
            margin: 0 auto;
            font-size: 14px;
            line-height: 20px;
            overflow: scroll;
        }
        p {
            margin-bottom: 10px;
        }
        input {
            border: 1px solid #3cc51f;
            padding: 5px 10px;
            margin: 0 10px 10px 0;
        }
    </style>
</head>
<body>
<div class="layui-card-body" style="padding: 15px;">
    <form class="layui-form" action="" lay-filter="operate-form-group">
        <div class="layui-tab layui-tab-brief">
            <div class="layui-tab-content">
                <div class="layui-form-item">
                    <label class="layui-form-label">渠道商ID</label>
                    <div class="layui-input-block">
                        <select name="channelMchId" id="channelMchId" lay-filter="channelMchId">
                            <option value="">请选择渠道商（*必选项）</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item" style="display: none" id="supChannelMchAgentIdTxt">
                    <label class="layui-form-label">渠道商上级代理ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="supChannelMchAgentId"  lay-verify="required" placeholder="" autocomplete="off" class="layui-input" disabled="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">支付产品</label>
                    <div class="layui-input-block">
                        <select name="payproduct" id="payproduct" lay-filter="payproduct">
                            <option value="">支付产品（*必选项）</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">支付通道</label>
                    <div class="layui-input-block">
                        <select name="paypassage" id="paypassage" lay-filter="paypassage">
                            <option value="">支付通道(*必选项)</option>
                            <!--<option value="1" id="selectChannelAgentMch" data-type="add">高级渠道代理商</option>-->
                            <!--<option value="2" class="selectChannelAgentMch" data-type="add">中级渠道代理商</option>-->
                            <!--<option value="3"  class="selectChannelAgentMch" data-type="add">普通渠道代理商</option>-->
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">渠道商费率(%)</label>
                   <div class="layui-input-block">
                        <input type="text" id="channelMchRate" name="channelMchRate" lay-verify="channelMchRate" placeholder="请输入渠道商费率（%）" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" style="display: none" id="supChannelAgentMchRateTxt">
                    <label class="layui-form-label">渠道商上级代理费率(%)</label>
                    <div class="layui-input-block">
                        <input type="text" id="supChannelAgentMchRate"  value="0" lay-verify="supChannelAgentMchRate" placeholder="" autocomplete="off" class="layui-input" disabled="">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">供应通道状态</label>
                        <div class="layui-input-inline">
                            <input type="radio" name="status" value="1" title="开启" checked="true">
                            <div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>
                                <div>开启</div>
                            </div>
                            <input type="radio" name="status" value="0" title="禁止">
                            <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                                <div>禁止</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="padding-bottom: 20px;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="channelMchSupplyAdd">新增</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script src="/x_mgr/start/layui/layui.js"></script>
<script>
    layui.use(['layer', 'form'], function(){
        var $ = layui.$
            ,layer = layui.layer

            ,index = parent.layer.getFrameIndex(window.name) //获取窗口索引
            ,element = layui.element
            ,form = layui.form;

        form.render(null, 'operate-form-group');
        /* 自定义验证规则 */
        form.verify({
            channelMchRate: function(value){
                var s = $('#supChannelAgentMchRate').val();

                console.log(s);
                console.log(value);

                if(value>s&&s!=0){
                    return '渠道商费率不得大于渠道商上级代理费率';
                }
            }
//            ,content: function(value){
//                layedit.sync(editIndex);
//            }
        });
        //获取渠道商ID，相关信息
        $.ajax({
            cache:false,
            type:"POST",
            url: '/api'+ '/channelmch_info/list',
            async:false,
            error:function(request){//请求失败之后的操作

            },
            success:function(res){//请求成功之后的操作
                if(res.code == 0) {
                    $("#channelMchId").find("option").remove();
                    $("#channelMchId").append('<option value="0">请选择渠道商分配供应通道(*必选项)</option>');
                    var channelMchList = res.data;
                    //遍历赋值
                    for(var i in channelMchList){
                        $("#channelMchId").append('<option value= '+channelMchList[i].channelMchId+'>'+ '['+'渠道商名称：'+channelMchList[i].channelMchName +']' +'---'+'[渠道商ID：'+channelMchList[i].channelMchId +']'+'</option>');

                    }
                    form.render('select');
                }
            }
        });
        var channelMchId;
        form.on('select(channelMchId)', function(data){
        //  alert(data.value); // 获取选中的值
           $("#supChannelMchAgentIdTxt").hide();
            channelMchId  = data.value;
            //获取渠道商ID，查找他是否有上级，如果没有上级就默认所有通道，都可以添加，如果有上级就只能分配他上级拥有的通道给他
            $.ajax({
                cache:false,
                type:"POST",
                url: '/api'+ '/channelmch_passage/getPayProductList',
                data: {
                    channelMchId: data.value,
                },
                async:false,
                error:function(request){//请求失败之后的操作

                },
                success:function(res){//请求成功之后的操作
                    if(res.code == 0) {
                        $("#payproduct").find("option").remove();
                        $("#payproduct").append('<option value="0">请选择渠道商分配支付产品(*必选项)</option>');
                        var channelMchProcuctList = res.data;
                        //遍历赋值
                        for(var i in channelMchProcuctList){
                            if(typeof(channelMchProcuctList[i].supChannelMchAgentId)!="undefined"){
                                $("#supChannelMchAgentIdTxt").show();
                                $("#supChannelMchAgentId").val(channelMchProcuctList[i].supChannelMchAgentId);
                            }else{
                                $("#supChannelMchAgentId").val(0);
                            }
                            if( $("#supChannelMchAgentId").val()==0) {
                                $("#payproduct").append('<option value= ' + channelMchProcuctList[i].payType + '>' + '[' + '支付产品：' + channelMchProcuctList[i].productName + ']' + '</option>');
                            }else{
                                $("#payproduct").append('<option value= ' + channelMchProcuctList[i].payType + '>' + '[' + '支付产品：' + channelMchProcuctList[i].productName + ']' + '---' + '[支付通道：' + channelMchProcuctList[i].passageName + ']' + '</option>');

                            }
                        }
                        form.render('select');
                    }
                }
            });

        });

        form.on('select(payproduct)', function(data){
           //alert(data.value); // 获取选中的值
            console.log(channelMchId);
            //获取支付产品中的支付类型，在根据支付类型，查询对应的支付通道
            $.ajax({
                cache:false,
                type:"POST",
                url: '/api'+ '/channelmch_passage/getPayPassageList',
                data: {
                    payType: data.value,
                    channelMchId:channelMchId
                },
                async:false,
                error:function(request){//请求失败之后的操作

                },
                success:function(res){//请求成功之后的操作
                    if(res.code == 0) {
                    //    alert(0);
                        $("#paypassage").find("option").remove();
                        $("#paypassage").append('<option value="0">请给渠道商分配支付通道(*必选项)</option>');
                        var channelMchAgentPaypassageList = res.data;
                        //遍历赋值
                        for(var i in channelMchAgentPaypassageList){
                            $("#paypassage").append('<option value= '+channelMchAgentPaypassageList[i].id+'>'+ '[支付通道]：' +channelMchAgentPaypassageList[i].passageName +'</option>');
                        }
                        form.render('select');
                    }
                }
            });
        });


        form.on('select(paypassage)', function(data){
        //    alert(data.value); // 获取选中的值
         //   alert( $("#supChannelMchAgentId").val()); // 获取选中的值
            var temp = $("#supChannelMchAgentId").val()
          //  alert($('#paypassage option:selected').val())
            console.log(data.value);

            if(temp!=0){
                $.ajax({
                    cache:false,
                    type:"POST",
                    url: '/api'+ '/channelmch_passage/getChannelSupRate',
                    data: {
                        paypassage: data.value,
                        channelAgentMchId:temp
                    },
                    async:false,
                    error:function(request){//请求失败之后的操作

                    },
                    success:function(res){//请求成功之后的操作
                        if(res.code == 0) {
                            $("#supChannelAgentMchRateTxt").show();
                            $('#supChannelAgentMchRate').val(res.data.channelAgentMchRate*100);
                          //  $("#supChannelAgentMchRate").val(res.date.channelAgentMchRate);
                        }
                    }
                });
            }
        });

        /* 监听提交 */
        form.on('submit(channelMchSupplyAdd)', function(data){
            console.log(JSON.stringify(data.field));
            $.ajax({
                cache:true,
                type:"POST",
                url: '/api'+ '/channelmch_passage/add',
                data:data.field,
                async:false,
                error:function(request){//请求失败之后的操作
                    layer.alert(res.msg, {title: "提示",icon: 6},function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);

                    });
                },
                success:function(res){//请求成功之后的操作
                    if(res.code == 0) {

                        layer.alert(res.msg, {title: "提示",icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);

                        });
                    }
                }
            });

        });

        $('.layui-btn-container .layui-btn').on('click', function(){
            var othis = $(this)
                ,type = othis.data('type');
            active[type] && active[type].call(this);
        });
    });
</script>
</body>
</html>
